package com.example.fw;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Set;

import com.example.tests.GroupObject;

public class DataBaseHelper extends HelperBase {

	private Connection conn;
       
	public DataBaseHelper(ApplicationManager manager) {
		super(manager);
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
         	} catch (Exception e) {
         		e.printStackTrace();
         		}	
		try {
			conn = DriverManager.getConnection(manager.getProperty("db.url"));
			} catch (SQLException e) {
				e.printStackTrace();
                }
        	}
       
	public String getLastGroupId() {
		Statement st = null;
		ResultSet res = null;
		try {
			st = conn.createStatement();
            res = st.executeQuery("SELECT MAX(group_id) FROM group_list");
            if (res.next()) {
            return res.getString(1);
            } else {
            	return null;
            	}
			} catch (SQLException e) {
				e.printStackTrace();
				return null;
                } finally {
                	try {
                		res.close();
                        st.close();
                        } catch (SQLException e) {
                        e.printStackTrace();
                        }
                }
        }

	public Set<GroupObject> getGroups() {
		Set<GroupObject> groups = new HashSet<GroupObject>();
        Statement st = null;
        ResultSet res = null;
        try {
        	st = conn.createStatement();
            res = st.executeQuery("SELECT group_id,group_name,group_header,group_footer FROM group_list");
            while (res.next()) {
            	GroupObject gr = new GroupObject()
                .setId(res.getString(1))
                .setName(res.getString(2))
                .setHeader(res.getString(3))
                .setFooter(res.getString(4));
            	groups.add(gr);
            	}
            } catch (SQLException e) {
            	e.printStackTrace();
            	} finally {
            		try {
            			res.close();
            			st.close();
            			} catch (SQLException e) {
            				e.printStackTrace();
            				}
            		}
        return groups;}
}